
//生成二维码
printCode = function (that) {
    var orderId = $(that).data('order');
    var code = $('.qrcode');
    $('.qrcode canvas').remove();
    $('.qrcode img').remove();
    $('.qrcode p').remove();
    var qrcode = new QRCode(document.getElementById('qrcode'), {
        text: orderId,
        width: 256,
        height: 256
    });
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        area: ['300px', '300px'],
        skin: '', //没有背景色
        shadeClose: true,
        content: $('.qrcode')
    });
    var html = "<p class='q-qrcode'>" + orderId + "</p>";
    code.append(html);
};

function clean(that) {
    var orderId = $(that).data('order');
    confirm = layer.confirm('是否对该出库单结算？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            url: 'index.php?g=Output&m=Bom&a=clean',
            data: {bomId: orderId},
            type: 'post',
            dateType: 'json',
            beforeSend: function () {
                layer.close(confirm);
                index = layer.load(0, {shade: false});
            }, complete: function () {
                layer.close(index);
            }, success: function (res) {
                if (res.success === true) {
                    layer.msg(res.message);
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                } else if (res.success === false) {
                    layer.alert(res.message);
                }
            }, error: function (e) {
                console.log(e);
            }
        });
    }, function () {
        layer.close(confirm);
    });
}

function setColumn() {
    if (storageUuid === '') {
        $('#table').bootstrapTable('showColumn', 'name');
    } else {
        $('#table').bootstrapTable('hideColumn', 'name');
    }
}

var orderId = null;
selectList = function (value, row) {
    var fen = row.fentryid ? ('+' + row.fentryid) : '';
    var uuid = row.uuid;
    orderId = row.order_id + fen;
    return '<a class="print" onclick="printCode(this)" href="javascript:;" data-order="' + row.order_id + fen + '"  style="text-decoration:none">二维码</a>' +
        '&nbsp&nbsp' + '<a  href="javascript:;" onclick="carList(this)"  data-uuid="' + row.uuid + '" style="text-decoration:none">投料单</a>' +
        "<select  style='margin-left: 5px' onchange=\"listChange(this,'" + uuid + "')\">" +
        '<option value="0">其余操作</option>' +
        '<option value="1">重推</option>' +
        '<option value="2">完成</option>' +
        '<option value="3">结算</option>' +
        '<option value="4">删除</option>' +
        '</select>';
};

listChange = function (obj, uuid) {
    var caseValue = obj.selectedIndex;
    var row = $('#table').bootstrapTable('getRowByUniqueId', uuid);
    $('#table select').val(0);
    switch (caseValue) {
        case 1:
            rePush(row);
            break;
        case 2:
            setting(row);
            break;
        case 3:
            balance(row);
            break;
        case 4:
            deleteBom(row);
            break;
    }
};

function deleteBom(row) {
    var uuid = row.uuid;
    var tip = '是否删除' + orderId + '订单？';
    var url = "{:url('Bom/deleteBom')}";
    var data = {bomUuid: uuid};
    reviseTable(tip, url, data);
}

function setting(row) {
    var uuid = row.uuid;
    var status = row.owe_status;
    if (status != 2) {
        layer.alert("只有在欠料状态下才能标记为完成!");
        return;
    }
    var tip = '是否将出库单' + orderId + '标记为完成？';
    var url = "{:url('Bom/setting')}";
    var data = {bomUuid: uuid};
    reviseTable(tip, url, data);
}

carList = function (that) {
    var bomUuid = $(that).data('uuid');
    var url = "{:url('Bom/car')}?history=1&bomUuid=" + bomUuid;
    window.location.href = url;
};

rePush = function (row) {
    var bomUuid = row.uuid;
    var data = {
        orderId: orderId,
        bomUuid: bomUuid
    };
    var tip = '是否重推出库单' + orderId;
    var url = "{:url('Bom/rePush')}";
    reviseTable(tip, url, data);
};

balance = function (row) {
    var bomUuid = row.uuid;
    var data = {
        orderId: orderId,
        bomUuid: bomUuid
    };
    var tip = '是否结算出库单' + orderId;
    var url = "{:url('Bom/balance')}";
    reviseTable(tip, url, data);
}